package org.dromara.tearoom.service.impl;

import org.apache.logging.log4j.util.Strings;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.tearoom.domain.bo.TerLockSerialnumBo;
import org.dromara.tearoom.domain.vo.KuodaoParam;
import org.dromara.tearoom.domain.vo.TerLockSerialnumVo;
import org.springframework.stereotype.Service;
import org.dromara.tearoom.domain.bo.TerDoorEventBo;
import org.dromara.tearoom.domain.vo.TerDoorEventVo;
import org.dromara.tearoom.domain.TerDoorEvent;
import org.dromara.tearoom.mapper.TerDoorEventMapper;
import org.dromara.tearoom.service.ITerDoorEventService;

import java.util.*;

/**
 * 茶室开门记录Service业务层处理
 *
 * @author xiechh
 * @date 2025-04-10
 */
@RequiredArgsConstructor
@Service
public class TerDoorEventServiceImpl implements ITerDoorEventService {

    private final TerDoorEventMapper baseMapper;
    private final KuodaoService kuodaoService;
    private final TerLockSerialnumServiceImpl terLockSerialnumService;

    /**
     * 查询茶室开门记录
     *
     * @param eventid 主键
     * @return 茶室开门记录
     */
    @Override
    public TerDoorEventVo queryById(Long eventid){
        return baseMapper.selectVoById(eventid);
    }

    /**
     * 分页查询茶室开门记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 茶室开门记录分页列表
     */
    @Override
    public TableDataInfo<TerDoorEventVo> queryPageList(TerDoorEventBo bo, PageQuery pageQuery) {
        KuodaoParam kuodaoParam = new KuodaoParam();
        if(bo.getHid()!=null&&bo.getHid()!=0)kuodaoParam.setRoomID(String.valueOf(bo.getHid()));
        kuodaoParam.setOffset((pageQuery.getPageNum()-1)*pageQuery.getPageSize());
        kuodaoParam.setCount(pageQuery.getPageSize());
        KuodaoParam returnkudao=kuodaoService.querydoorRecord(kuodaoParam);
        if(returnkudao==null)return null;

        List<LinkedHashMap> kuodaolist =returnkudao.getEventList();
        TerLockSerialnumBo tlbo = new TerLockSerialnumBo();
        tlbo.setDelFlag("0");
        List<TerLockSerialnumVo> nolist= terLockSerialnumService.queryList(tlbo);
        List<TerDoorEventVo> voList=new ArrayList<>();
        TerDoorEventVo vo=null;
        for(TerLockSerialnumVo nvo:nolist){
            for(LinkedHashMap map:kuodaolist){
                if(nvo.getHid()==null)continue;
                if(nvo.getHid().equals(map.get("roomID").toString())){
                    vo = new TerDoorEventVo();
                    vo.setTeroomName(nvo.getTeroomName());
                    vo.setOwner(map.get("owner")!=null?map.get("owner").toString():"");
                    vo.setEventID(map.get("eventID").toString());
                    vo.setEventTime(map.get("eventTime").toString());
                    vo.setRoomID(map.get("roomID").toString());
                    voList.add(vo);
                }
            }
        }
        return TableDataInfo.build(voList);
    }

    /**
     * 查询符合条件的三方开门记录列表
     *
     * @param bo 查询条件
     * @return 茶室开门记录列表
     */
    @Override
    public List<TerDoorEventVo> queryList(TerDoorEventBo bo) {
        KuodaoParam kuodaoParam = new KuodaoParam();
        if(bo.getHid()!=null&&bo.getHid()!=0)kuodaoParam.setRoomID(String.valueOf(bo.getHid()));

        KuodaoParam returnkudao=kuodaoService.querydoorRecord(kuodaoParam);
        if(returnkudao==null)return null;

        List<LinkedHashMap> kuodaolist =returnkudao.getEventList();
        TerLockSerialnumBo tlbo = new TerLockSerialnumBo();
        tlbo.setDelFlag("0");
        List<TerLockSerialnumVo> nolist= terLockSerialnumService.queryList(tlbo);
        List<TerDoorEventVo> voList=new ArrayList<>();
        TerDoorEventVo vo=null;
        for(TerLockSerialnumVo nvo:nolist){
            for(LinkedHashMap map:kuodaolist){
                if(nvo.getHid()==null)continue;
                if(nvo.getHid().equals(map.get("roomID").toString())){
                    vo = new TerDoorEventVo();
                    vo.setTeroomName(nvo.getTeroomName());
                    vo.setOwner(map.get("owner")!=null?map.get("owner").toString():"");
                    vo.setEventID(map.get("eventID").toString());
                    vo.setEventTime(map.get("eventTime").toString());
                    vo.setRoomID(map.get("roomID").toString());
                    voList.add(vo);
                }
            }
        }
        return voList;
    }

    private LambdaQueryWrapper<TerDoorEvent> buildQueryWrapper(TerDoorEventBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TerDoorEvent> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(TerDoorEvent::getEventid);
        lqw.eq(bo.getHid() != null, TerDoorEvent::getHid, bo.getHid());
        lqw.eq(StringUtils.isNotBlank(bo.getOwner()), TerDoorEvent::getOwner, bo.getOwner());
        lqw.eq(bo.getEventTime() != null, TerDoorEvent::getEventTime, bo.getEventTime());
        lqw.like(StringUtils.isNotBlank(bo.getTeroomName()), TerDoorEvent::getTeroomName, bo.getTeroomName());
        return lqw;
    }

    /**
     * 新增茶室开门记录
     *
     * @param bo 茶室开门记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TerDoorEventBo bo) {
        TerDoorEvent add = MapstructUtils.convert(bo, TerDoorEvent.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setEventid(add.getEventid());
        }
        return flag;
    }

    /**
     * 修改茶室开门记录
     *
     * @param bo 茶室开门记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TerDoorEventBo bo) {
        TerDoorEvent update = MapstructUtils.convert(bo, TerDoorEvent.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TerDoorEvent entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除茶室开门记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
